package com.ymatou.api.sellerpromotion.testcase;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.api.sellerpromotion.service.GetPromotionByIdCall;
import com.ymatou.api.sellerpromotion.service.GetPromotionProductByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.database.model.YmtSellerPromotionProducts;
import com.ymttest.database.mongo.SellerPromotionInfoWapper;
import com.ymttest.database.sqlwapper.YmtSellerPromotionProductsIMapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;



public class Ts_GetPromotionById {
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据促销ID、买手ID获取促销商品信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetPromotionByIdCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_001() {
		Logger.start(true, "获取进行中-指定商品促销信息与mongo一致");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsSpecifyProduct",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 获取mongo 数据
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			boolean IsShowProducts = true;
			JSONObject GetPromotionByIdBean = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId,"IsShowProducts",IsShowProducts));
			
			Logger.verifyEquals(200,GetPromotionByIdBean.getInt("Code"), "响应报文-返回码");
			// 校验mongo 数据库数据
			JSONObject PromotionInfoBean = GetPromotionByIdBean.getJSONObject("Data").getJSONObject("PromotionInfo");
			
			Logger.verifyEquals(PromotionInfoBean.getInt("SellerId"), mongoDataMap.get("SellerId"), "买手ID");
			Logger.verifyEquals(PromotionInfoBean.getString("PromotionName"), mongoDataMap.get("PromotionName"), "满减促销名称");
			Logger.verifyEquals(PromotionInfoBean.getInt("PromotionType"), mongoDataMap.get("PromotionType"), "满减促销类型");
			Logger.verifyEquals(PromotionInfoBean.getString("PromotionDesc"), mongoDataMap.get("PromotionDesc"), "满减促销描述");
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(PromotionInfoBean.getString("PromotionBeginTime"))), (Date)mongoDataMap.get("PromotionBeginTime"), 60);
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(PromotionInfoBean.getString("PromotionEndTime"))), (Date)mongoDataMap.get("PromotionEndTime"), 60);
			Logger.verifyEquals(PromotionInfoBean.getInt("PromotionLimitNum"), mongoDataMap.get("PromotionLimitNum"), "满减促销买家限购数");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("CanUseSellerCoupon"), mongoDataMap.get("CanUseSellerCoupon"), "满减促销是否使用买手优惠券");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsSameProductUse"), mongoDataMap.get("IsSameProductUse"), "是否同商品满减");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsSpecifyProduct"), mongoDataMap.get("IsSpecifyProduct"), "是否指定商品活动");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsLimitProduct"), mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");	
			Logger.verifyEquals(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("QtyCondition"), new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getInt("QtyCondition"), "满减促销件数");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getString("Value")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("Value")), "优惠折扣");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("AmountCondition")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("AmountCondition")), "优惠折扣");
			
			JSONArray prodSqlArray = new JSONArray();
			for(int i = 0;i<PromotionInfoBean.getJSONArray("PromotionProducts").length();i++){
				prodSqlArray.put(PromotionInfoBean.getJSONArray("PromotionProducts").getJSONObject(i).get("ProductId"));
			}
			
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			AssertService.checkArray(productIds,prodSqlArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_002() {
		Logger.start(true, "获取进行中-排除商品的促销信息验证数据与数据库一致");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","2");
			map.put("PromotionInfo->IsLimitProduct",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 获取mongo 数据
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			boolean IsShowProducts = true;
			JSONObject GetPromotionByIdBean = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId,"IsShowProducts",IsShowProducts));
			
			Logger.verifyEquals(200,GetPromotionByIdBean.getInt("Code"), "响应报文-返回码");
			// 校验商品ID是否正确
			JSONObject PromotionInfoBean = GetPromotionByIdBean.getJSONObject("Data").getJSONObject("PromotionInfo");
			
			Logger.verifyEquals(PromotionInfoBean.getInt("SellerId"), mongoDataMap.get("SellerId"), "买手ID");
			Logger.verifyEquals(PromotionInfoBean.getString("PromotionName"), mongoDataMap.get("PromotionName"), "满减促销名称");
			Logger.verifyEquals(PromotionInfoBean.getInt("PromotionType"), mongoDataMap.get("PromotionType"), "满减促销类型");
			Logger.verifyEquals(PromotionInfoBean.getString("PromotionDesc"), mongoDataMap.get("PromotionDesc"), "满减促销描述");
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(PromotionInfoBean.getString("PromotionBeginTime"))), (Date)mongoDataMap.get("PromotionBeginTime"), 60);
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(PromotionInfoBean.getString("PromotionEndTime"))), (Date)mongoDataMap.get("PromotionEndTime"), 60);
			Logger.verifyEquals(PromotionInfoBean.getInt("PromotionLimitNum"), mongoDataMap.get("PromotionLimitNum"), "满减促销买家限购数");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("CanUseSellerCoupon"), mongoDataMap.get("CanUseSellerCoupon"), "满减促销是否使用买手优惠券");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsSameProductUse"), mongoDataMap.get("IsSameProductUse"), "是否同商品满减");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsSpecifyProduct"), mongoDataMap.get("IsSpecifyProduct"), "是否指定商品活动");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsLimitProduct"), mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");	
			Logger.verifyEquals(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("QtyCondition"), new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getInt("QtyCondition"), "满减促销件数");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getString("Value")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("Value")), "优惠折扣");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("AmountCondition")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("AmountCondition")), "优惠金额");
			
			JSONArray prodSqlArray = new JSONArray();
			for(int i = 0;i<PromotionInfoBean.getJSONArray("PromotionProducts").length();i++){
				prodSqlArray.put(PromotionInfoBean.getJSONArray("PromotionProducts").getJSONObject(i).get("ProductId"));
			}
			
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			AssertService.checkArray(productIds,prodSqlArray);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_003() {
		Logger.start(true, "获取进行中-全场商品促销商品信息");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满20享8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","3");
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 获取mongo 数据
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			
			
			boolean IsShowProducts = true;
			JSONObject GetPromotionByIdBean = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId,"IsShowProducts",IsShowProducts));
			
			Logger.verifyEquals(200,GetPromotionByIdBean.getInt("Code"), "响应报文-返回码");
			// 校验mongo 数据库数据
			JSONObject PromotionInfoBean = GetPromotionByIdBean.getJSONObject("Data").getJSONObject("PromotionInfo");
			
			Logger.verifyEquals(PromotionInfoBean.getInt("SellerId"), mongoDataMap.get("SellerId"), "买手ID");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsSpecifyProduct"), mongoDataMap.get("IsSpecifyProduct"), "是否指定商品活动");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsLimitProduct"), mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");	
			Logger.verifyEquals(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("QtyCondition"), new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getInt("QtyCondition"), "满减促销件数");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getString("Value")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("Value")), "优惠折扣");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("AmountCondition")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("AmountCondition")), "优惠金额");
			
			JSONArray prodSqlArray = new JSONArray();
			for(int i = 0;i<PromotionInfoBean.getJSONArray("PromotionProducts").length();i++){
				prodSqlArray.put(PromotionInfoBean.getJSONArray("PromotionProducts").getJSONObject(i).get("ProductId"));
			}
			
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			AssertService.checkArray(productIds,prodSqlArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_004() {
		Logger.start(true, "获取进行中-指定商品促销商品信息-不展示商品信息");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","3");
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 校验商品ID是否正确
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			 
			boolean IsShowProducts = false;
			JSONObject GetPromotionByIdBean = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId,"IsShowProducts",IsShowProducts));
			
			Logger.verifyEquals(200,GetPromotionByIdBean.getInt("Code"), "响应报文-返回码");
			// 校验mongo 数据库数据
			JSONObject PromotionInfoBean = GetPromotionByIdBean.getJSONObject("Data").getJSONObject("PromotionInfo");
			Logger.verifyIsNull(PromotionInfoBean.getString("PromotionProducts"), "查询商品信息为空");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_005() {
		Logger.start(false, "获取未开始满减促销信息,查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件享8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(1));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","4");
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 查询mongo 信息
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			int  PromotionState = (int) mongoDataMap.get("PromotionState");
			if(PromotionState==0){	
			} else{
				return;
			}
			JSONObject GetPromotionByIdBean = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId,"IsShowProducts",true));
			Logger.verifyEquals(200,GetPromotionByIdBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyIsNull(GetPromotionByIdBean.getJSONObject("Data").getString("PromotionInfo"), "查询结果为空");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionById_006() {
		Logger.start(false, "获取已结束满减促销中的商品ID,查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件享8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(1));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsLimitProduct",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			//取消活动
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 查询mongo 信息
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			Logger.verifyIsNull(mongoDataMap, "mongo 中没有数据");
			JSONObject GetPromotionByIdBean = new GetPromotionByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId,"IsShowProducts",true));
			Logger.verifyEquals(200,GetPromotionByIdBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyIsNull(GetPromotionByIdBean.getJSONObject("Data").getString("PromotionInfo"), "查询结果为空");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	

}
